/**
 * @param {String} str (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
 */
function dateTimeStr(str){
	let time = new Date();
	time = time.setSeconds(time.getSeconds()+3600);
	let date = new Date(time);
	let year = date.getFullYear(); //年
	let month = date.getMonth() + 1; //月
	let day = date.getDate(); //日
	let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //时
	let minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(); //分
	let second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
	let dateTime = Date.parse(date) / 1000;
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	hour >= 0 && hour <= 9 ? hour : "";
	minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	second >= 0 && second <= 9 ? (second = "0" + second) : "";
	const weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
	if(str.indexOf('y') != -1){
		str = str.replace('y', year)
	}
	if(str.indexOf('m') != -1){
		str = str.replace('m', month)
	}
	if(str.indexOf('d') != -1){
		str = str.replace('d', day)
	}
	if(str.indexOf('h') != -1){
		str = str.replace('h', hour)
	}
	if(str.indexOf('i') != -1){
		str = str.replace('i', minute)
	}
	if(str.indexOf('s') != -1){
		str = str.replace('s', second)
	}
	// 获取星期几
	const currentWeek = weekday[date.getDay()];
	return {str, dateTime, currentWeek};
}
// 格式转换  (y-m-d h:i:s) y:年 m:月 d:日 h:时 i:分 s:秒
function timeFormat(time, str="y-m-d h:i:s") {
	let date = new Date(time);
	if(time instanceof Date) {
		date = time;
	} else {
		date = new Date(time);
	}
	
	let year = date.getFullYear(); //年
	let month = date.getMonth() + 1; //月
	let day = date.getDate(); //日
	let hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //时
	let minute = date.getMinutes() < 10 ? date.getMinutes() : date.getMinutes(); //分
	let second = date.getSeconds() < 10 ? date.getSeconds() : date.getSeconds(); //秒
	let dateTime = Date.parse(date) / 1000;
	month >= 1 && month <= 9 ? (month = "0" + month) : "";
	day >= 0 && day <= 9 ? (day = "0" + day) : "";
	hour >= 0 && hour <= 9 ? hour : "";
	minute >= 0 && minute <= 9 ? (minute = "0" + minute) : "";
	second >= 0 && second <= 9 ? (second = "0" + second) : "";
	const weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
	if(str.indexOf('y') != -1){
		str = str.replace('y', year)
	}
	if(str.indexOf('m') != -1){
		str = str.replace('m', month)
	}
	if(str.indexOf('d') != -1){
		str = str.replace('d', day)
	}
	if(str.indexOf('h') != -1){
		str = str.replace('h', hour)
	}
	if(str.indexOf('i') != -1){
		str = str.replace('i', minute)
	}
	if(str.indexOf('s') != -1){
		str = str.replace('s', second)
	}
	// 获取星期几
	const currentWeek = weekday[date.getDay()];
	return {str, dateTime, currentWeek};
}




// 获取N天后的时间
function getLaterDate(dayNum, dateTime = null) {
	// 如果为null,则格式化当前时间为时间戳
	if (!dateTime) dateTime = +new Date();
	// 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
	if (dateTime.toString().length == 10) dateTime *= 1000;
	const timestamp = +new Date(Number(dateTime));
		
	const one_day = 86400000; // 24 * 60 * 60 * 1000;
	const addVal = dayNum * one_day + timestamp;
	//x天后的日期
	const date = new Date(addVal);
		
	//格式化日期
	const filters = n => {
		return n < 10 ? (n = '0' + n) : n;
	};
	const month = filters(date.getMonth() + 1);
	const day = filters(date.getDate());
	const hours = filters(date.getHours());
	const minutes = filters(date.getMinutes());
	const seconds = filters(date.getSeconds());
	// const lastTime = `${date.getFullYear()}/${month}/${day} ${hours}:${minutes}:${seconds}`;
	const lastTime = `${month}月${day}日  ${hours}:${minutes}`;
	const weekday = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
	// 获取星期几
	const dayOfWeek = date.getDay();
	const lastdayOfWeek = weekday[dayOfWeek];
	const lastDateTime = Date.parse(date) / 1000;
	return {lastTime, lastdayOfWeek, lastDateTime};
}

// 将时间戳转化为时分秒的格式，一般用作倒计时
function timeDownFn (st,et){
	let countdown = {
		day:'',
		hour:'',
		miun:'',
		swc:''
	}
	// const startTime = +new Date(st)
	// const endTime = +new Date(et)
	// 倒计时总秒数
	const timesall = (et-st) / 1000;
	const hours = parseInt(timesall / 3600);
	const minutes = parseInt((timesall % 3600) / 60);
	const seconds = parseInt(timesall % 60)
	return hours+'时'+minutes+'分'+seconds+'秒';
}

// 防抖
function throttle(fn, wait) {
    let delay = wait || 500;
    let timer = null
    return function() {
        if (timer) {
            console.log('拦截');
            return
        }
        timer = setTimeout(() => {
            console.log('发送');
            fn.apply(this, arguments)
            timer = null
        }, delay)
    }
}
module.exports = {
	dateTimeStr,
	getLaterDate,
	timeDownFn,
	throttle,
	timeFormat
}